/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.home.gui;

import com.home.common.ApplicationCommon;
import com.home.components.ModalJInternalFrame;
import com.home.item.ScorecardTypeComboItem;
import com.home.mapping.Agent;
import com.home.mapping.Scorecard;
import com.home.mapping.ScorecardType;
import com.home.mapping.Venture;
import java.awt.Component;
import java.util.Map;
import javax.swing.ButtonGroup;
import javax.swing.JComponent;
import javax.swing.JRadioButton;

/**
 *
 * @author pnduy
 */
public class ChooseScorecardTypeDialog extends ModalJInternalFrame {
    
    private ScorecardTypeComboItem[] scorecardTypeItem;
    private MainScreen main;
    private Agent agentSelected;
    private Venture currentVenture;
    private Map<Integer,Agent> mapAgents;
    private ScorecardType choosenType;
    private ScorecardType defaultType;
    private QAWorkingList qaWorkList;
    /**
     * Creates new form ChooseScorecardTypeDialog
     */
    public ChooseScorecardTypeDialog(MainScreen main, ScorecardTypeComboItem[] scorecardTypeItem, Agent agentSelected, Venture currentVenture, Map<Integer,Agent> mapAgents, ScorecardType defaultType, QAWorkingList qaWorkList) {
        initComponents();
        
        setBounds(ApplicationCommon.getRectangleForCenter(main.getSize().width, main.getSize().height, this.getWidth(), this.getHeight()));
        
        this.scorecardTypeItem = scorecardTypeItem;
        this.main = main;
        this.agentSelected = agentSelected;
        this.currentVenture = currentVenture;
        this.mapAgents = mapAgents;
        this.defaultType = defaultType;
        this.qaWorkList = qaWorkList;
        this.setModal(true);
        loadData();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panelType = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        btnChooser = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setClosable(true);
        setTitle("Choose Scorecard Type");
        setPreferredSize(new java.awt.Dimension(430, 320));

        panelType.setBorder(javax.swing.BorderFactory.createTitledBorder("Scorecard Type"));
        panelType.setLayout(new javax.swing.BoxLayout(panelType, javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Command"));

        btnChooser.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_ok.png"))); // NOI18N
        btnChooser.setText("Choose");
        btnChooser.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnChooserActionPerformed(evt);
            }
        });

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icon/btn_cancel.png"))); // NOI18N
        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(176, Short.MAX_VALUE)
                .addComponent(btnCancel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnChooser)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnChooser)
                    .addComponent(btnCancel))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(panelType, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelType, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnChooserActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnChooserActionPerformed
        for (Component radCom : panelType.getComponents()) {
            if (radCom instanceof JRadioButton) {
                JRadioButton rad = (JRadioButton) radCom;
                if (rad.isSelected()) {
                    for (ScorecardTypeComboItem item : scorecardTypeItem) {
                        String id = item.getValue().getIdScorecardType().toString();

                        if (id.equals(rad.getActionCommand())) {
                            choosenType = item.getValue();
                        }
                    }
                }
            }
        }
        this.dispose();
        main.addNewInternalFrame(new AgentScorecard(main, choosenType, agentSelected, currentVenture, new Scorecard(), mapAgents, qaWorkList));
        
    }//GEN-LAST:event_btnChooserActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnChooser;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel panelType;
    // End of variables declaration//GEN-END:variables
    
    private void loadData() {
        ButtonGroup group = new ButtonGroup();
        boolean checked = false;
        for (ScorecardTypeComboItem item : scorecardTypeItem) {
            JRadioButton rad = new JRadioButton(item.getValue().getType());
            rad.setActionCommand(item.getValue().getIdScorecardType()+"");
            // set default
            if (item.getValue().getIdScorecardType().compareTo(defaultType.getIdScorecardType()) == 0) {
                rad.setSelected(true);
                checked = true;
            }
            group.add(rad);                                    
            panelType.add(rad);
        }
        if (!checked) {
            group.getElements().nextElement().setSelected(true);
        }
    }
}
